package com.airport.ticket.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.airport.ticket.common.DateUtil;
import com.airport.ticket.dao.UserInfoMapper;
import com.airport.ticket.entity.UserInfo;
import com.airport.ticket.entity.UserInfoExample;

@Controller
@RequestMapping("/")
public class LoginController {

	@Autowired
	private UserInfoMapper userInfoMapper;
	
	@Autowired
	private HttpServletRequest request;
	
	@RequestMapping(value = "/")
	public String index()  {
        return "redirect:login.html";
    }
	
	@RequestMapping(value = "login.html")
    public String login(ModelMap map){
        return "login";
    }
	
	@RequestMapping(value = "register.html")
    public String register(ModelMap map){
        return "register";
    }
	
	@RequestMapping(value = "logout.html")
    public String loginOut(ModelMap map){
		HttpSession session = request.getSession();
		session.removeAttribute("loginUserInfo");
        return "redirect:/login.html";
    }
	
	@PostMapping(value = "login_check.html")
    public ModelAndView loginCheck(ModelAndView mv, UserInfo user){
		
		/**
		 * 验密
		 */
		UserInfoExample example = new UserInfoExample();
		example.createCriteria().andUsernameEqualTo(user.getUsername()).andPasswordEqualTo(user.getPassword());
		List<UserInfo> userInfoList = userInfoMapper.selectByExample(example);
		
		if(userInfoList==null || userInfoList.size()==0 || userInfoList.get(0).getId()==null || "".equals(userInfoList.get(0).getId())) {
			mv.setViewName("login");
			mv.addObject("retCode", "99");
			mv.addObject("msg", "用户名或密码错误");
	        return mv;
		}
		
		HttpSession session = request.getSession();
		session.setAttribute("loginUserInfo", userInfoList.get(0));
		
		mv.setViewName("redirect:/admin/");		
        return mv;
    }
	
	@PostMapping(value = "do_register.html")
    public ModelAndView doRegister(ModelAndView mv, UserInfo user){
		
		/**
		 * 查看用户名是否已存在
		 */
		UserInfoExample example = new UserInfoExample();
		example.createCriteria().andUsernameEqualTo(user.getUsername());
		List<UserInfo> userInfoList = userInfoMapper.selectByExample(example);
		
		if(userInfoList!=null && userInfoList.size()>0) {
			mv.setViewName("login");
			mv.addObject("retCode", "99");
			mv.addObject("msg", "用户名已存在");
	        return mv;
		}
		
		user.setCreatetime(new Date());
		user.setUserType("2");
		user.setAmount(new BigDecimal("5000.00"));
		
		userInfoMapper.insert(user);
		
		mv.setViewName("login");
		mv.addObject("retCode", "99");
		mv.addObject("msg", "注册成功，请登录");
        return mv;
    }
	
}
